package poidemo.docx4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.zeroturnaround.zip.ZipUtil;

public class DocxReplace {

	public static void addHexinId(String docxFile) throws Exception {

		byte[] bytes = ZipUtil.unpackEntry(new File(docxFile), "word/document.xml");

		SAXReader saxReader = new SAXReader();
		Document xml = saxReader.read(new ByteArrayInputStream(bytes));
//		AtomicLong acc = new AtomicLong(1);
//		Element rootElement = xml.getRootElement();
		xml.addComment("www.10jqka.com.cn");
		addIds(xml);
		ByteArrayOutputStream out = new ByteArrayOutputStream(
				(int) (bytes.length * 1.2));
		OutputStreamWriter writer = new OutputStreamWriter(out);
		XMLWriter xmlWriter = new XMLWriter(writer,
				OutputFormat.createCompactFormat());
		xmlWriter.write(xml);
		xmlWriter.flush();
		byte[] bytesOut = out.toByteArray();
		ZipUtil.replaceEntry(new File(docxFile), "word/document.xml", bytesOut);
		FileUtils.writeByteArrayToFile(new File("out1.xml"), bytes);
		FileUtils.writeByteArrayToFile(new File("out2.xml"), bytesOut);
	}

	private static void addIds(Document xml) {
		AtomicLong acc = new AtomicLong(1);
		Element rootElement = xml.getRootElement();
		addIds(rootElement, acc);
	}

	private static int level = 0;

	private static void addIds(Element element, AtomicLong acc) {

		if (element == null) {
			return;
		}

		for (int i = 0; i < level; i++) {
			System.out.print("   ");
		}

		// 如果有hexinId节点，则先移除掉
		Iterator<Element> iter = element.elementIterator();
		while (iter.hasNext()) {
			Element next = iter.next();
			if (next.getQualifiedName().equals("hexinId")) {
				next.detach();
			}
		}
		// 获取hexinId
		String hexinId = String.valueOf(acc.getAndIncrement());
//		element.addAttribute("hexinId", hexinId);
		// 创建hexinId节点
		Element hexinId1 = element.addElement("hexinId", "");
		hexinId1.addAttribute("hexinId", hexinId);
		System.out.println(element.getText());
		// 遍历
		iter = element.elementIterator();
		while (iter.hasNext()) {
			Element next = iter.next();
			// 跳过新创建的节点
			if (next == hexinId1) {
				continue;
			}
			level++;
			addIds(next, acc);
			level--;
		}
	}

	public static void addHexinId2(String docxFile) throws Exception {

		FileInputStream fin = new FileInputStream(new File(docxFile));
		ZipInputStream zipIn = new ZipInputStream(fin);
		ZipEntry document = null;
		while ((document = zipIn.getNextEntry()) != null) {
			String line = String.format(
					"ZipEntry(%s, isDirectory=%d, size=%d, compressedSize=%d, time=%d, crc=%d, method=%d, comment=%s)",
					document.getName(), (document.isDirectory()) ? 1 : 0,
					document.getSize(), document.getCompressedSize(), document.getTime(),
					document.getCrc(), document.getMethod(), document.getComment());
			System.out.println(line);
			if ("word/document.xml".equals(document.getName())) {
				break;
			}
		}
	}

	public static void main(String[] args) throws Exception {
		addHexinId("D:\\tmp\\docx\\test61 - 副本.docx");
	}

}
